<?php

/*
    Class   : CSV ( Comma Separated Values )
    Author  : Suwandi Tan
    
    Copyright (C) 2011  Suwandi Tan ( swndtan@gmail.com )

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */


class CSV
{
    private $results;
    
    private function loadcsv($file, $delim = ',')
    {
        $csv = array();
        if (($fp = fopen($file, "r")) !== FALSE) 
        {
            $csv [] = fgetcsv($fp, 1000, $delim);
            
            while (($data = fgetcsv($fp, 1000, $delim)) !== FALSE) { $csv [] = $data; }
            
            fclose($fp);
        }
        
        return $csv;
    }
    
    public function load($file, $delimiter = ',')
    {
        $rows = $this->loadcsv($file, $delimiter);
        $this->results = array();
        $columns = array();

        foreach ($rows as $key => $row)
        {
            if ($key == 0)
            {
                $columns = $row;
                continue;
            }

            foreach ($columns as $columns_key => $column)
            {
                $this->results[$key][$column] = $row[$columns_key];
            }
        }

        return array_merge($this->results);
    }
    
    public function join($data, $relation = array(), $delim = ',')
    {
        if(sizeof($relation) <=1)
        {
            $relatedFrom = $relation[0];
            $relatedTo = $relation[0];
        }
        else
        {
            $relatedFrom = $relation[0];
            $relatedTo = $relation[1];
        }
        
        $rows = $data;
        $columns = array();
        
        $data = new ArrayIterator($rows);
        
        $origin = $this->results;
        $this->results = array();
        
        foreach ($origin as $key => $row)
        {
            $filter = new FieldFilter($data);

            $filter->field = $relatedTo;
            $filter->search = $row[$relatedFrom];

            foreach($filter as $key => $val)
            {
                $row[$relatedFrom] = $val;
            }
            
            $this->results [] = $row;
        }
        
        return $this->results;
    }
    
    public function find($search = array())
    {
        $data = new ArrayIterator($this->results);
        $filter = new FieldFilter($data);
        
        $results = array();
        foreach($search as $key => $val)
        {
            $filter->field = $key;
            $filter->search = $val;

            foreach($filter as $key => $val)
            {
                $results [] = $val;
            }
        }
        
        return $results;
    }
    
    public function select($select = array())
    {
        $data = $this->results;
        
        if(isset($select['where']))
        {
            $data = $this->find($select['where']);
        }
        
        $filter = new SelectFilter($data);
        $filter->select($select['select']);
        
        return $filter->crawl();
    }
}

